package utils

import (
	"log"
	"os"
	"sync"
)

// Logger 日志记录器
type Logger struct {
	debugLogger *log.Logger
	infoLogger  *log.Logger
	warnLogger  *log.Logger
	errorLogger *log.Logger
}

var (
	// GlobalLogger 全局日志记录器
	GlobalLogger *Logger
	loggerOnce   sync.Once
)

func init() {
	loggerOnce.Do(func() {
		GlobalLogger = &Logger{
			debugLogger: log.New(os.Stdout, "[DEBUG] ", log.LstdFlags),
			infoLogger:  log.New(os.Stdout, "[INFO] ", log.LstdFlags),
			warnLogger:  log.New(os.Stdout, "[WARN] ", log.LstdFlags),
			errorLogger: log.New(os.Stderr, "[ERROR] ", log.LstdFlags),
		}
	})
}

// Debug 记录调试日志
func (l *Logger) Debug(format string, v ...interface{}) {
	l.debugLogger.Printf(format, v...)
}

// Info 记录信息日志
func (l *Logger) Info(format string, v ...interface{}) {
	l.infoLogger.Printf(format, v...)
}

// Warning 记录警告日志
func (l *Logger) Warning(format string, v ...interface{}) {
	l.warnLogger.Printf(format, v...)
}

// Error 记录错误日志
func (l *Logger) Error(format string, v ...interface{}) {
	l.errorLogger.Printf(format, v...)
}

// Fatal 记录致命错误日志并退出程序
func (l *Logger) Fatal(format string, v ...interface{}) {
	l.errorLogger.Printf(format, v...)
	os.Exit(1)
}
